package org.study.swing.study01.component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年11月15日
 */
public class DialogDemo1 {
    public static void main(String[] args) {
        // testDialog1();
        // testDialog2();
        // new MyDialogDemo1();
        new MyDialogDemo2();
    }

    private static void testDialog1() {
        JDialog dialog = new JDialog();
        dialog.setTitle("示例：模态对话框");
        dialog.setModal(true);
        JLabel label = new JLabel("This is a modal dialog");
        dialog.add(label);
        dialog.setSize(300, 200);
        // 将窗口居中显示在屏幕上
        dialog.setLocationRelativeTo(null);
        // 可选值：DO_NOTHING_ON_CLOSE、HIDE_ON_CLOSE、DISPOSE_ON_CLOSE
        dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    }

    private static void testDialog2() {
        JFrame frame = new JFrame("示例：模态对话框");
        Container frameContainer = frame.getContentPane();

        JButton button = new JButton("打开对话框");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JDialog dialog = new JDialog(frame, "打开的模态框", true);
                Container dialogContainer = dialog.getContentPane();
                dialog.setSize(300, 200);
                dialog.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println("模态框被关闭了");
                        dialog.dispose();
                    }
                });
                JLabel label = new JLabel("This is a modal dialog");
                dialogContainer.add(label, BorderLayout.CENTER);
                dialog.setLocationRelativeTo(frame);
                dialog.setVisible(true);
            }
        });
        frameContainer.add(button, BorderLayout.CENTER);

        frame.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    /**
     * 监听按钮点击事件：当用户点击对话框中的按钮时，需要执行相应的操作。这可以通过为按钮添加ActionListener来实现
     */
    static class MyDialogDemo1 {
        public MyDialogDemo1() {
            JFrame frame = new JFrame("示例：模态对话框");
            Container frameContainer = frame.getContentPane();

            JButton button = new JButton("打开对话框");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    openDialog(frame);
                }
            });
            frameContainer.add(button, BorderLayout.CENTER);

            frame.setSize(600, 400);
            // 将窗口居中显示在屏幕上
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }

        private static void openDialog(JFrame frame) {
            JDialog dialog = new JDialog(frame, "打开的模态框", true);
            dialog.setSize(300, 200);

            JButton okButton = new JButton("确定");
            okButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("【确定】按钮被点击了");
                    dialog.dispose();
                }
            });
            JButton cancelButton = new JButton("取消");
            cancelButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("【取消】按钮被点击了");
                    dialog.dispose();
                }
            });
            JPanel buttonPanel = new JPanel();
            buttonPanel.add(okButton);
            buttonPanel.add(cancelButton);

            dialog.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
            dialog.setLocationRelativeTo(frame);
            dialog.setVisible(true);
        }
    }

    /**
     * 监听对话框关闭事件：当用户点击对话框的关闭按钮或者按下Esc键时，可能需要执行一些操作。这可以通过为对话框添加WindowListener来实现。
     */
    static class MyDialogDemo2 {
        public MyDialogDemo2() {
            JFrame frame = new JFrame("示例：模态对话框");
            Container frameContainer = frame.getContentPane();

            JButton button = new JButton("打开对话框");
            button.addActionListener(e -> openDialog(frame));
            frameContainer.add(button, BorderLayout.CENTER);

            frame.setSize(600, 400);
            // 将窗口居中显示在屏幕上
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }

        private static void openDialog(JFrame frame) {
            JDialog dialog = new JDialog(frame, "打开的模态框", true);
            dialog.setSize(200, 100);

            dialog.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.out.println("模态框被关闭了");
                    dialog.dispose();
                }
            });

            JLabel label = new JLabel("This is a modal dialog");
            dialog.getContentPane().add(label, BorderLayout.CENTER);
            dialog.setLocationRelativeTo(frame);
            dialog.setVisible(true);
        }
    }
}
